package com.ee.chat.api.task;

import com.ee.chat.api.common.CommonService;
import com.ee.chat.common.dao.UserMapper;
import com.ee.chat.common.dao.UserSealLogMapper;
import com.ee.chat.common.entity.User;
import com.ee.chat.common.entity.UserSealLog;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 解除封禁状态
 * @Author Shane
 * @Date 2020/8/5 15:10
 */
@Component
public class SealClearTask {
    @Resource
    UserSealLogMapper userSealLogMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    CommonService commonService;

    /**
     * 解除临时封禁的用户，1分钟执行一次
     * @Author Shane
     * @Date 2020/8/5 15:13
     * @param
     * @return
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    @Transactional(rollbackFor = Exception.class)
    public void clear() throws Exception{
        if(!commonService.getIpStatus()){
            return;
        }

        List<UserSealLog> userSealLogs = userSealLogMapper.selectSealLogs();
        for(UserSealLog userSealLog : userSealLogs){
            User user = new User();
            user.setId(userSealLog.getUserId());
            user.setStatus(0);

            //解除封禁状态
            userMapper.updateById(user);

            //更新记录
            userSealLog.setStatus(1);
            userSealLogMapper.updateById(userSealLog);
        }
    }
}
